package com.liyghting.springbootmybatisdemo.order.controller;

import com.liyghting.springbootmybatisdemo.order.mapper.OrderItemMapper;
import com.liyghting.springbootmybatisdemo.order.mapper.OrderMapper;
import com.liyghting.springbootmybatisdemo.order.model.Order;
import com.liyghting.springbootmybatisdemo.order.model.OrderItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiYiGuang
 * @date 2018/10/23 16:25
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private OrderItemMapper orderItemMapper;

	@GetMapping("/insert")
	@Transactional
	public Map insert(HttpServletRequest request) {
		log.info("order insert request {}", request);
		Map<String, String> map = new HashMap<>();
		map.put("1", "hello world");
		Order order = new Order();
		order.setStatus("A 完成");
		order.setUserId(1);

		int i = orderMapper.insert(order);
		if (i != 1) {
			map.put(i + "", "order A fail");
			return map;
		}
		OrderItem orderItem = new OrderItem();
		orderItem.setOrderId(order.getOrderId());
		orderItem.setStatus("A 完成");
		orderItem.setUserId(1);
		i = orderItemMapper.insert(orderItem);
		if (i != 1) {
			map.put(i + "", "order A fail");
			return map;
		}
		map.put(i + "", "A ok");
		return map;
	}
}
